<template>
  <el-dialog
    title="提示"
    width="400px"
    :visible.sync="visible"
    append-to-body
    :close-on-press-escape="false"
    :close-on-click-modal="false"
    :show-close="false"
  >
    <div class="box">
      <div class="conf-box">
        <div class="conf-box-title">
          您是否已确定按清单人员进行分配？
        </div>
        <ul class="msg-1">
          <li>确定之后，额度将即可到达员工账户</li>
          <li>分配成功后，如有调整，可进行回收处理</li>
        </ul>
      </div>
      <div class="conf-box">
        <div class="conf-box-title">输入操作密码</div>
        <div class="msg-2">请输入登录账号{{ phone }}的操作密码</div>
        <el-form ref="form">
          <el-form-item ref="passItem" prop="operationPassword" :error="passError">
            <el-input
              v-model.trim="form.operationPassword"
              show-password
              placeholder="请输入操作密码"
              @input="passChange"
            />
          </el-form-item>
        </el-form>
        <div v-if="isClient" class="lose-pass-item">
          <el-button type="text" @click="onForget">设置/找回操作密码</el-button>
        </div>
      </div>
      <div v-if="showProgress" class="conf-box">
        <div class="conf-box-title">等待发放结果</div>
        <el-progress :text-inside="true" :stroke-width="16" :percentage="percentage"></el-progress>
        <div v-show="second > 0" class="msg-3">{{ second }}s后自动关闭</div>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button :disabled="loading" @click="cancel">取消</el-button>
      <el-button type="primary" :loading="loading" @click="ok">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { mapGetters } from 'vuex';
  import { commitDistribute } from '@/api/careCard';
  import { getRequestTag, getProgressBar } from '@/api/base';
  import { passWordJsEncrypt } from '@/utils/base';

  export default {
    data() {
      return {
        visible: false,
        loading: false,
        form: {
          id: 0,
          operationPassword: '',
        },
        passError: '',
        showProgress: false,
        percentage: 0,
        requestTag: '',
        timer: null,
        second: 0, //计数3次，自动退出
      };
    },
    computed: {
      ...mapGetters('user', ['userInfo']),
      isClient() {
        return !!this.userInfo.isClient || false;
      },
      phone() {
        const phone = (this.userInfo && this.userInfo.phone) || '';
        return phone ? phone.replace(/^(\d{3})\d{5}(\d{3})$/, '$1*****$2') : '';
      },
    },
    methods: {
      open(id) {
        this.visible = true;
        this.form.operationPassword = '';
        this.form.id = id;
        this.passError = '';
        this.second = 0;
        this.percentage = 0;
        this.showProgress = false;
        this.loading = false;
      },
      cancel() {
        this.visible = false;
        this.form.operationPassword = '';
        this.form.id = 0;
        this.passError = '';
        this.second = 0;
        this.percentage = 0;
        this.showProgress = false;
        this.loading = false;
      },
      async queryProgress() {
        const { data = 0 } = await getProgressBar(this.requestTag);
        this.percentage = data;
      },
      async ok() {
        this.loading = true;
        const res = await getRequestTag();
        this.requestTag = res.data;
        this.timer = setInterval(this.queryProgress, 1000);
        // 延时1.5s显示进入条
        const delayTimer = setTimeout(() => {
          this.showProgress = true;
        }, 1500);
        const { status, message } = await commitDistribute({
          ...this.form,
          operationPassword: passWordJsEncrypt(this.form.operationPassword),
          requestTag: this.requestTag,
        });
        clearTimeout(delayTimer);
        clearInterval(this.timer);
        if (status === 500) {
          this.showProgress = false;
          this.$nextTick(() => {
            this.passError = message;
          });
          this.loading = false;
        } else {
          this.second = 3;
          const countTimer = setInterval(() => {
            this.second = this.second - 1;
            if (this.second === 0) {
              clearInterval(countTimer);
              this.cancel();
              this.$emit('on-ok');
            }
          }, 1000);
        }
      },
      onForget() {
        // 调用客户端服务
        const { ipcRenderer } = window.require('electron');
        const param = {
          type: 3, //打开客户端对应模块
          path: 2, // 1修改薪享卡薪礼卡支付密码, 2 操作密码
        };
        ipcRenderer.sendToHost(JSON.stringify(param));
        this.cancel();
      },
      passChange() {
        this.passError = '';
      },
    },
  };
</script>

<style lang="scss" scoped>
  $color: #7f848b;
  .conf-box-title {
    line-height: 24px;
    font-size: 14px;
    color: #1e2023;
    text-align: center;
    font-weight: bold;
    margin-bottom: 10px;
  }
  .msg-1 {
    color: $color;
    padding-left: 56px;
    margin-bottom: 30px;
  }
  .msg-2 {
    color: $color;
    margin-bottom: 20px;
    text-align: center;
  }
  .msg-3 {
    color: $color;
    text-align: center;
    padding-top: 2px;
  }
  .lose-pass-item {
    text-align: right;
  }
  .box {
    // padding: 15px 40px;
  }
</style>
